package cn.edu.xjtu.nbrash.xbarsuite.floatbutton;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;
import android.preference.SwitchPreference;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.xjtu.nbrash.xbarsuite.R;
import cn.edu.xjtu.nbrash.xbarsuite.RashIntents;
import cn.edu.xjtu.nbrash.xbarsuite.utilities.RootUtil;

public class RashFloatButton extends Activity{

	@SuppressWarnings("unused")
	private static final String TAG = RashFloatButton.class.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//加上titlebar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
//		RelativeLayout rLayout = new RelativeLayout(this);
//		TextView textview = new TextView(this);
//		textview.setText("悬浮按钮");
//		textview.setTextSize(20);
//		RelativeLayout.LayoutParams relLayoutParams=new RelativeLayout.LayoutParams
//				(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
//		relLayoutParams.height=50;
//		relLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
//		relLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
//		rLayout.setBackgroundColor(R.drawable.title_bar);
//		rLayout.addView(textview, relLayoutParams);
		setContentView(R.layout.activity_titlebar_floatbutton);
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.beginTransaction()
				.replace(android.R.id.content, new FloatButtonPreference())
				.commit();
	}
	public void back(View v){
			finish();
}

	public static class FloatButtonPreference extends PreferenceFragment {
		
		@Override
		public void onCreate(Bundle savedInstanceState) {

			super.onCreate(savedInstanceState);
			addPreferencesFromResource(R.xml.float_button);

			final SharedPreferences sharedPreferences = getActivity()
					.getSharedPreferences(
							RashIntents.FloatButton.FLOAT_BUTTON_PREFERENCES,
							Context.MODE_PRIVATE);

			final SwitchPreference switchPreference = (SwitchPreference) getPreferenceScreen()
					.findPreference("float_button_state");
			switchPreference
					.setChecked(FloatButtonService.isFloatButtonExist());

			switchPreference
					.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
						@Override
						public boolean onPreferenceChange(
								Preference preference, Object newValue) {

							boolean checking = (Boolean) newValue;
							if (((SwitchPreference) preference).isChecked() != checking) {
								if (checking) {
									// 打开
									if (!RootUtil.checkRootPermission()) {
										makeAToast("请检查手机是否具有root权限！");
									}
									sharedPreferences
											.edit()
											.putBoolean(
													RashIntents.FloatButton.FLOAT_BUTTON_PERMISSION,
													RashIntents.FloatButton.FLOAT_BUTTON_PERMISSION_ALLOWED)
											.commit();
									if (!FloatButtonService
											.isFloatButtonExist()) {
										Intent service = new Intent(getView()
												.getContext()
												.getApplicationContext(),
												StickyService.class);
										getView().getContext()
												.getApplicationContext()
												.startService(service);

									} else {
										makeAToast("做人不要贪心，你已经有一个了！");
									}
								} else {
									// 关掉
									sharedPreferences
											.edit()
											.putBoolean(
													RashIntents.FloatButton.FLOAT_BUTTON_PERMISSION,
													RashIntents.FloatButton.FLOAT_BUTTON_PERMISSION_DENIED)
											.commit();
									FloatButtonService
											.startActionRemoveFloatButton(getView()
													.getContext());
								}
							}

							return true;
						}
					});
		}

		private void makeAToast(String msg) {
			Toast.makeText(getView().getContext().getApplicationContext(), msg,
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
